{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "bd6151ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import time\n",
    "from scipy import stats\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "97b9e1d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(r'F:\\我的文件夹\\拉勾数据分析实战训练营(预习资料)\\作业\\阶段七模块二作业\\模块二作业\\ab_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7e88ff13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id                   timestamp      group landing_page  converted\n",
       "0   851104  2017-01-21 22:11:48.556739    control     old_page          0\n",
       "1   804228  2017-01-12 08:01:45.159739    control     old_page          0\n",
       "2   661590  2017-01-11 16:55:06.154213  treatment     new_page          0\n",
       "3   853541  2017-01-08 18:28:03.143765  treatment     new_page          0\n",
       "4   864975  2017-01-21 01:52:26.210827    control     old_page          1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()\n",
    "#一级指标：人均停留时长(人均停留时长之差作为统计量)\n",
    "#二级指标：点击率（广告点击量之差作为统计量）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2c8dde3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "data['date']=data.timestamp.str[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9602ca8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "control=data.converted[(data.group=='control')&(data.landing_page=='old_page')].mean()\n",
    "control #对照组的点击率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1d1faa3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11880724790277405"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "treatment=data.converted[(data.group=='treatment')&(data.landing_page=='new_page')].mean()\n",
    "treatment#实验组的点击率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1a3bcdc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "alpha=0.05\n",
    "belta=0.2\n",
    "z_alpha =stats.norm.ppf(1-alpha)\n",
    "z_belta =stats.norm.ppf(1-belta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a3644a95",
   "metadata": {},
   "outputs": [],
   "source": [
    "n=control*(1-control)*(z_alpha+z_belta*np.sqrt(treatment*(1-treatment)/(control*(1-control)))/(treatment-control))**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7bdd157d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29556.415070666048"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n #最小样本量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "01d57cf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "group      landing_page\n",
       "control    new_page          1928\n",
       "           old_page        145274\n",
       "treatment  new_page        145311\n",
       "           old_page          1965\n",
       "Name: user_id, dtype: int64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby([\"group\",\"landing_page\"])[\"user_id\"].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "aaa9ef8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#二级指标的假设检验\n",
    "#原假设：treatment-control<=0\n",
    "#备择假设：treatment-control>0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4db1d0ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.8495118192726028"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算Z值\n",
    "z=(treatment-control)/np.sqrt(control*(1-control)/145274)\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ddc15050",
   "metadata": {},
   "outputs": [],
   "source": [
    "# p值为0.05时 ，(左侧)z=-1.655\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "614e1291",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_p = data.converted[(data.group=='control')&(data.landing_page=='old_page')|(data.group=='treatment')&(data.landing_page=='new_page')].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "71096ad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11959667567149027"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c651af82",
   "metadata": {},
   "outputs": [],
   "source": [
    "z=(treatment-control)/np.sqrt(all_p*(1-all_p)*(1/145274+1/145311))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e52f0ac9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.3116075339133115"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d2ea33e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#在z分布中左侧当alpaha=0.05时 z=1.96,当z<=-1.96或z>=1.96时，拒绝原假设\n",
    "z = -1.31 > -1.96\n",
    "#无法拒绝原假设"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "8a64ac1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#封装如下\n",
    "def ABtest(df: pd.DataFrame, group_col: str = None, value_col: str = None):\n",
    "    if not group_col:\n",
    "        group_col = df.columns[0]\n",
    "    if not value_col:\n",
    "        value_col = df.columns[1]\n",
    "        \n",
    "    temp = df.groupby(group_col,as_index=False)[value_col].mean()\n",
    "    temp_n = df.groupby(group_col,as_index=False)[value_col].count()\n",
    "    all_p = df[value_col].mean()\n",
    "    tongjiliang =temp.iloc[0,1] - temp.iloc[1,1]\n",
    "    \n",
    "    z=(temp.iloc[0,1] - temp.iloc[1,1])/np.sqrt(all_p*(1-all_p)*(1/temp_n.iloc[0,1]+1/temp_n.iloc[1,1]))\n",
    "#在z分布中左侧当alpaha=0.05时 z=1.96,当z<=-1.96或z>=1.96时，拒绝原假设\n",
    "    if abs(z) <1.96:\n",
    "        print('无法拒绝原假设')\n",
    "    else:\n",
    "        print('拒绝原假设')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "6b51515d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无法拒绝原假设\n"
     ]
    }
   ],
   "source": [
    "temp  = data.loc[((data.group==\"control\") & (data.landing_page==\"old_page\")) \n",
    "                  | ((data.group==\"treatment\") & (data.landing_page==\"new_page\")),[\"group\",\"converted\"]]\n",
    "ABtest(temp)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
